package com.hongtech.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hongtech.common.bean.AjaxResultVo;
import com.hongtech.common.page.PageData;
import com.hongtech.framework.anno.OperateLog;
import com.hongtech.framework.anno.Permission;
import com.hongtech.modules.common.BaseController;
import com.hongtech.modules.system.dto.RegionDTO;
import com.hongtech.modules.system.entity.RegionEntity;
import com.hongtech.modules.system.service.RegionService;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 地区
 *
 * @author lch 123456789@qq.com
 * @since 1.0.0 2023-09-16
 */
@Controller
@RequestMapping("/system/region")
public class RegionController extends BaseController {

    @Autowired
    private RegionService regionService;

    @Permission("system:region:list")
    @RequestMapping("/list")
    public String list() {
        return "/system/region/list";
    }

    @Permission("system:region:list")
    @RequestMapping("/page")
    @ResponseBody
    public AjaxResultVo page(Integer offset, Integer limit) {
        LambdaQueryWrapper<RegionEntity> ew = new LambdaQueryWrapper<>();
        Map<String, Object> pageParams = getPageParam(limit, offset);
        PageData<RegionDTO> pages = regionService.pages(pageParams, ew);
        AjaxResultVo resultVo = AjaxResultVo.success();
        resultVo.put("total", pages.getTotal());
        resultVo.put("rows", pages.getList());
        return resultVo;
    }

    @Permission("system:region:save")
    @RequestMapping("/add")
    public String add() {
        return "/system/region/add";
    }

    @Permission("system:region:save")
    @OperateLog("创建地区")
    @RequestMapping("/doAdd")
    @ResponseBody
    public AjaxResultVo doAdd(RegionDTO dto) {
        regionService.add(dto);
        return AjaxResultVo.success();
    }

    @Permission("system:region:edit")
    @RequestMapping("/edit/{id}")
    public String edit(@PathVariable Long id, Model model) {
        RegionDTO dto = regionService.get(id);
        model.addAttribute("entity", dto);
        return "/system/region/edit";
    }

    @Permission("system:region:edit")
    @OperateLog("编辑地区")
    @RequestMapping("/doEdit")
    @ResponseBody
    public AjaxResultVo doEdit(RegionDTO dto) {
        regionService.update(dto);
        return AjaxResultVo.success();
    }

    @Permission("system:region:remove")
    @OperateLog("删除地区")
    @RequestMapping("/delete")
    @ResponseBody
    public AjaxResultVo delete(Long id) {
        regionService.removeById(id);
        return AjaxResultVo.success();
    }

}
